<script type="text/html" template>
    <link rel="stylesheet" href="{{ layui.setter.paths.core }}css/login.css?v={{ layui.admin.v }}" media="all">
</script>


<div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="display: none;">
    <div class="layadmin-user-login-main">
        <div class="layadmin-user-login-box layadmin-user-login-header">
            <h2>忘记密码</h2>
            <p></p>
        </div>
        <div class="layadmin-user-login-box layadmin-user-login-body layui-form" lay-filter="LAY-user-login">

            <script type="text/html" template>
                {{# if(layui.router().search.type === 'resetpass'){ }}
                <!--                <div class="layui-form-item">-->
                <!--                    <label class="layadmin-user-login-icon layui-icon layui-icon-password"-->
                <!--                           for="LAY-user-login-password"></label>-->
                <!--                    <input type="password" name="new_password" id="LAY-user-login-password" lay-verify="pass"-->
                <!--                           placeholder="新密码" class="layui-input">-->
                <!--                </div>-->
                <!--                <div class="layui-form-item">-->
                <!--                    <label class="layadmin-user-login-icon layui-icon layui-icon-password"-->
                <!--                           for="LAY-user-login-repass"></label>-->
                <!--                    <input type="password" name="repass" id="LAY-user-login-repass" lay-verify="required"-->
                <!--                           placeholder="确认密码" class="layui-input">-->
                <!--                </div>-->
                <!--                <div class="layui-form-item">-->
                <!--                    <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="LAY-user-forget-resetpass">-->
                <!--                        重置新密码-->
                <!--                    </button>-->
                <!--                </div>-->
                {{# } else { }}
                <div class="layui-form-item">
                    <label class="layadmin-user-login-icon layui-icon layui-icon-email"
                           for="LAY-user-login-email"></label>
                    <!--                    <input type="text" name="email" id="LAY-user-login-email" lay-verify="required|email"-->
                    <!--                           placeholder="请输入注册时的邮箱" class="layui-input" value="raingor2024@163.com">-->
                    <input type="text" name="username" id="LAY-user-login-email" lay-verify="required|email"
                           placeholder="请输入注册时的邮箱" class="layui-input"
                    >
                </div>
                <!--                <div class="layui-form-item">-->
                <!--                    <div class="layui-row">-->
                <!--                        <div class="layui-col-xs7">-->
                <!--                            <label class="layadmin-user-login-icon layui-icon layui-icon-vercode"-->
                <!--                                   for="LAY-user-login-vercode"></label>-->
                <!--                            <input type="text" name="vercode" id="LAY-user-login-vercode" lay-verify="required"-->
                <!--                                   placeholder="图形验证码" class="layui-input">-->
                <!--                        </div>-->
                <!--                        <div class="layui-col-xs5">-->
                <!--                            <div style="margin-left: 10px;">-->
                <!--                                <img src="https://www.oschina.net/action/user/captcha"-->
                <!--                                     class="layadmin-user-login-codeimg" id="LAY-user-get-vercode">-->
                <!--                            </div>-->
                <!--                        </div>-->
                <!--                    </div>-->
                <!--                </div>-->
                <div class="layui-form-item">
                    <div class="layui-row">
                        <div class="layui-col-xs7">
                            <label class="layadmin-user-login-icon layui-icon layui-icon-vercode"
                                   for="LAY-user-login-smscode"></label>
                            <input type="text" name="code" id="LAY-user-login-smscode" lay-verify="required"
                                   placeholder="邮箱验证码" class="layui-input">
                        </div>
                        <div class="layui-col-xs5">
                            <div style="margin-left: 10px;">
                                <button type="button" class="layui-btn layui-btn-primary layui-btn-fluid"
                                        id="LAY-user-forget-getsmscode">获取验证码
                                </button>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layadmin-user-login-icon layui-icon layui-icon-password"
                           for="LAY-user-login-password"></label>
                    <input type="password" name="new_password" id="LAY-user-login-password" lay-verify="pass"
                           placeholder="新密码" class="layui-input">
                </div>
                <div class="layui-form-item">
                    <label class="layadmin-user-login-icon layui-icon layui-icon-password"
                           for="LAY-user-login-repass"></label>
                    <input type="password" name="repass" id="LAY-user-login-repass" lay-verify="required"
                           placeholder="确认密码" class="layui-input">
                </div>
                <div class="layui-form-item" style="margin-bottom: 20px;">
                    <a lay-href="/user/login" class="layadmin-user-jump-change layadmin-link" style="margin-top: 7px;">返回登录</a>
                </div>
                <div class="layui-form-item">
                    <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="LAY-user-forget-submit">重置密码
                    </button>
                </div>
                {{# } }}
            </script>

        </div>
    </div>

    <div class="layui-trans layadmin-user-login-footer">
        <p>© All Rights Reserved</p>
    </div>

</div>
<script>
    layui.use(['admin', 'form', 'user'], function () {
        var $ = layui.$
            , setter = layui.setter
            , admin = layui.admin
            , form = layui.form
            , router = layui.router();

        form.render();

        //发送邮箱验证码
        let sendEmailVerify = function () {
            var isValid = form.validate('#LAY-user-login-email');  // 主动触发验证，v2.7.0 新增
            // 验证通过
            if (isValid) {
                // 此处可继续书写「发送验证码」等后续逻辑
                // …
                let v = "layui-disabled"
                    , elem = $('#LAY-user-forget-getsmscode')
                    , seconds = 59 // 倒数秒数
                elem.addClass(v).off('click')

                let t = setInterval(() => {
                    if (seconds === 0) {
                        elem.text("获取验证码")
                        elem.removeClass(v).on('click', sendEmailVerify)
                        clearInterval(t)
                    } else {
                        elem.text(seconds + "秒后再次发送");
                        seconds--;
                    }
                }, 1000)

                var formData = form.val('LAY-user-login');
                // 发送邮箱验证码
                admin.req({
                    url: window.globalConfig.url_email_verify,
                    type: "post",
                    data: {
                        email: formData.username,
                        scene: 3
                    },
                    success: (res) => {
                        layer.msg(res.msg)
                    }
                })
            }
        }

        setTimeout(() => {
            $('#LAY-user-forget-getsmscode').on('click', sendEmailVerify);
        }, 500)

        //找回密码下一步
        form.on('submit(LAY-user-forget-submit)', function (obj) {
            var field = obj.field;
            field.scene = 3
            delete field.repass

            // 密码重置
            admin.req({
                url: window.globalConfig.url_password + "/1"
                , type: "PUT"
                , data: field
                , done: function (res) {
                    // let _phone = CryptoJS.AES.encrypt(field.phone, "ctmon").toString()
                    // location.hash = router.href + '/type=resetpass/phone=' + _phone;
                    layer.msg('密码已成功重置', {
                        offset: '15px'
                        , icon: 1
                        , time: 1000
                    }, function () {
                        location.hash = '/user/login'; //跳转到登入页
                    });

                }
            });

            return false;
        });

        //重置密码
        // form.on('submit(LAY-user-forget-resetpass)', function (obj) {
        //     var field = obj.field;
        //
        //     //确认密码
        //     if (field.new_password !== field.repass) {
        //         return layer.msg('两次密码输入不一致');
        //     }
        //     var bytes = CryptoJS.AES.decrypt(layui.router().search.phone, 'ctmon');
        //     var _phone = bytes.toString(CryptoJS.enc.Utf8);
        //     field.phone = _phone
        //     // console.log(field)
        //     //请求接口
        //     admin.req({
        //         url: window.globalConfig.url_password //实际使用请改成服务端真实接口
        //         , data: field
        //         , done: function (res) {
        //
        //             // layer.msg('密码已成功重置', {
        //             //   offset: '15px'
        //             //   ,icon: 1
        //             //   ,time: 1000
        //             // }, function(){
        //             //   location.hash = '/user/login'; //跳转到登入页
        //             // });
        //         }
        //     });
        //     return false;
        // });

    });
</script>